﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class TimingProject
    {
        public int Add(Chain.Model.TimingProject model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into TimingProject(");
            builder.Append("ProjectName,ProjectCategoryID,ProjectRulesID,ProjectAddTime,ProjectShopID,ProjectUserID,ProjectRemark)");
            builder.Append(" values (");
            builder.Append("@ProjectName,@ProjectCategoryID,@ProjectRulesID,@ProjectAddTime,@ProjectShopID,@ProjectUserID,@ProjectRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectName", SqlDbType.NVarChar, 50), new SqlParameter("@ProjectCategoryID", SqlDbType.Int, 4), new SqlParameter("@ProjectRulesID", SqlDbType.Int, 4), new SqlParameter("@ProjectAddTime", SqlDbType.DateTime), new SqlParameter("@ProjectShopID", SqlDbType.Int, 4), new SqlParameter("@ProjectUserID", SqlDbType.Int, 4), new SqlParameter("@ProjectRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.ProjectName;
            cmdParms[1].Value = model.ProjectCategoryID;
            cmdParms[2].Value = model.ProjectRulesID;
            cmdParms[3].Value = model.ProjectAddTime;
            cmdParms[4].Value = model.ProjectShopID;
            cmdParms[5].Value = model.ProjectUserID;
            cmdParms[6].Value = model.ProjectRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ProjectID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from TimingProject ");
            builder.Append(" where ProjectID=@ProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProjectID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ProjectIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from TimingProject ");
            builder.Append(" where ProjectID in (" + ProjectIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ProjectID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from TimingProject");
            builder.Append(" where ProjectID=@ProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProjectID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsCategory(int CategoryID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from TimingProject");
            builder.Append(" where EXISTS(SELECT 1 FROM TimingCategory WHERE TimingCategory.CategoryID = TimingProject.ProjectCategoryID \r\n                           AND (TimingCategory.CategoryFatherID = @CategoryFatherID OR TimingCategory.CategoryID = @CategoryID))");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryFatherID", SqlDbType.Int, 4), new SqlParameter("@CategoryID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CategoryID;
            cmdParms[1].Value = CategoryID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsRules(int RulesID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from TimingProject");
            builder.Append(" where ProjectRulesID=@ProjectRulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectRulesID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RulesID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ProjectID,ProjectName,ProjectCategoryID,ProjectRulesID,ProjectAddTime,ProjectShopID,ProjectUserID,ProjectRemark ");
            builder.Append(" FROM TimingProject ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ProjectID,ProjectName,ProjectCategoryID,ProjectRulesID,ProjectAddTime,ProjectShopID,ProjectUserID,ProjectRemark ");
            builder.Append(" FROM TimingProject ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ProjectID desc");
            }
            builder.Append(")AS Row, T.*  from TimingProject T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int MemID, int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "TimingProject,SysUser,Timingrules";
            string[] column = new string[] { string.Format("TimingProject.*,SysUser.UserName,Timingrules.RulesName,Timingrules.RulesRemark,Timingrules.RulesID,ISNULL((SELECT SUM(StorageResidueTime) FROM MemStorageTiming  WHERE TimingProject.ProjectID = MemStorageTiming.StorageTimingProjectID AND MemStorageTiming.StorageTimingMemID = {0}),0) AS AllCount", MemID) };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ProjectAddTime", "ProjectID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.TimingProject GetModel(int ProjectID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ProjectID,ProjectName,ProjectCategoryID,ProjectRulesID,ProjectAddTime,ProjectShopID,ProjectUserID,ProjectRemark from TimingProject ");
            builder.Append(" where ProjectID=@ProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProjectID;
            Chain.Model.TimingProject project = new Chain.Model.TimingProject();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ProjectID"] != null) && (set.Tables[0].Rows[0]["ProjectID"].ToString() != ""))
            {
                project.ProjectID = int.Parse(set.Tables[0].Rows[0]["ProjectID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectName"] != null) && (set.Tables[0].Rows[0]["ProjectName"].ToString() != ""))
            {
                project.ProjectName = set.Tables[0].Rows[0]["ProjectName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ProjectCategoryID"] != null) && (set.Tables[0].Rows[0]["ProjectCategoryID"].ToString() != ""))
            {
                project.ProjectCategoryID = int.Parse(set.Tables[0].Rows[0]["ProjectCategoryID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectRulesID"] != null) && (set.Tables[0].Rows[0]["ProjectRulesID"].ToString() != ""))
            {
                project.ProjectRulesID = int.Parse(set.Tables[0].Rows[0]["ProjectRulesID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectAddTime"] != null) && (set.Tables[0].Rows[0]["ProjectAddTime"].ToString() != ""))
            {
                project.ProjectAddTime = DateTime.Parse(set.Tables[0].Rows[0]["ProjectAddTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectShopID"] != null) && (set.Tables[0].Rows[0]["ProjectShopID"].ToString() != ""))
            {
                project.ProjectShopID = int.Parse(set.Tables[0].Rows[0]["ProjectShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectUserID"] != null) && (set.Tables[0].Rows[0]["ProjectUserID"].ToString() != ""))
            {
                project.ProjectUserID = int.Parse(set.Tables[0].Rows[0]["ProjectUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProjectRemark"] != null) && (set.Tables[0].Rows[0]["ProjectRemark"].ToString() != ""))
            {
                project.ProjectRemark = set.Tables[0].Rows[0]["ProjectRemark"].ToString();
            }
            return project;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM TimingProject ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.TimingProject model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update TimingProject set ");
            builder.Append("ProjectName=@ProjectName,");
            builder.Append("ProjectCategoryID=@ProjectCategoryID,");
            builder.Append("ProjectRulesID=@ProjectRulesID,");
            builder.Append("ProjectAddTime=@ProjectAddTime,");
            builder.Append("ProjectShopID=@ProjectShopID,");
            builder.Append("ProjectUserID=@ProjectUserID,");
            builder.Append("ProjectRemark=@ProjectRemark");
            builder.Append(" where ProjectID=@ProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProjectName", SqlDbType.NVarChar, 50), new SqlParameter("@ProjectCategoryID", SqlDbType.Int, 4), new SqlParameter("@ProjectRulesID", SqlDbType.Int, 4), new SqlParameter("@ProjectAddTime", SqlDbType.DateTime), new SqlParameter("@ProjectShopID", SqlDbType.Int, 4), new SqlParameter("@ProjectUserID", SqlDbType.Int, 4), new SqlParameter("@ProjectID", SqlDbType.Int, 4), new SqlParameter("@ProjectRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.ProjectName;
            cmdParms[1].Value = model.ProjectCategoryID;
            cmdParms[2].Value = model.ProjectRulesID;
            cmdParms[3].Value = model.ProjectAddTime;
            cmdParms[4].Value = model.ProjectShopID;
            cmdParms[5].Value = model.ProjectUserID;
            cmdParms[6].Value = model.ProjectID;
            cmdParms[7].Value = model.ProjectRemark;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

